<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车队名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="车队名称"></el-input>
    </el-form-item>
    <el-form-item label="" prop="fleetNumber">
      <el-input v-model="dataForm.fleetNumber" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="所属机构" prop="agencyId">
      <el-input v-model="dataForm.agencyId" placeholder="所属机构"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="manager">
      <el-input v-model="dataForm.manager" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="状态 0：禁用 1：正常" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 0：禁用 1：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          fleetNumber: '',
          agencyId: '',
          manager: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '车队名称不能为空', trigger: 'blur' }
          ],
          fleetNumber: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          agencyId: [
            { required: true, message: '所属机构不能为空', trigger: 'blur' }
          ],
          manager: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0：禁用 1：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/fleet/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.fleet.name
                this.dataForm.fleetNumber = data.fleet.fleetNumber
                this.dataForm.agencyId = data.fleet.agencyId
                this.dataForm.manager = data.fleet.manager
                this.dataForm.status = data.fleet.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/fleet/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'fleetNumber': this.dataForm.fleetNumber,
                'agencyId': this.dataForm.agencyId,
                'manager': this.dataForm.manager,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
